package com.liuyh.demo.serviceconsumer.service;

import com.liuyh.demo.serviceconsumer.client.AddClient;
import com.liuyh.demo.serviceconsumer.client.DecreaseClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ComputeService {
    @Autowired
    AddClient addClient;

    @Autowired
    DecreaseClient decreaseClient;

    @HystrixCommand(fallbackMethod = "addServiceFallback")
    public int addService(int a,int b){
        try {
//            logger.debug("sleeping ...................");
            Thread.sleep(RandomUtils.nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       return addClient.add(a,b);
    }

    //服务级别hystrix
    public int addServiceFallback(int a,int b){
        return 88888;
    }

    public int decreaseService(int a,int b){
        try {
//            logger.debug("sleeping ...................");
            Thread.sleep(RandomUtils.nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return decreaseClient.decrease(a,b);
    }

}
